package jt.classic.app.info;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.utils.AnnexTool;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.StringTool;
import org.limp.mine.annex.AnnexInfo;
import org.limp.mine.annex.AnnexItem;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 新闻图片管理Action
 *
 * @author Bizkit
 *
 */
public class InfoIMGAction extends DispatchAction {
    /**
    * 插入图片到新闻
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return
    * @throws Exception
    */
    public ActionForward insertimg(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ bean = new CommonBean4HSQ(request);
        Map para = bean.getResource();

        String uniqueid = (String) para.get("uniqueid");

        AnnexItem item = (AnnexItem) para.get(AnnexItem.ANNEX);

        if (!item.isEmpty()) {
            item.setObjectId(uniqueid);
            item.setObjectName("INSERT_IMG");

            try {
                SystemDataSource ds = SystemDataSource.getInstance();

                conn = ds.getConnection();

                AnnexTool tool = new AnnexTool(conn);

                List list = tool.saveAnnex(item, false);

                conn.commit();

                AnnexInfo aitemp = tool.getAnnex(((AnnexInfo) list.get(0)).getID());

                //AnnexInfo ai = tool.cloneAnnex(aitemp.getID());
                request.setAttribute("img", aitemp);
            } catch (Exception ex) {
                ISystem.catchException(this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        }

        return mapping.findForward("insertimg");
    }

    /**
     * 删除信息中的图片
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward deleteimg(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ bean = new CommonBean4HSQ(request);
        Map para = bean.getResource();

        String url = StringTool.checkString(para.get("oimgurl"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();

            conn = ds.getConnection();

            InfoManager manager = new InfoManager(conn);

            manager.cleanImageInInfo(url);

            conn.commit();

            request.setAttribute("result", "1");
        } catch (Exception ex) {
            request.setAttribute("result", "-1");
            ISystem.catchException(this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("deleteimg");
    }
}
